﻿/// <reference path="../jQuery/jquery.js" />

function searchOxford(value) {
	var url = 'http://oald8.oxfordlearnersdictionaries.com/dictionary/' + value;

	$.ajax({
		url: url,
		type: 'GET',
		beforeSend: function () {
			jQuery("#oxfordAjax").toggle(true);
		},
		success: function (res) {
			try {
				var response = $(res.responseText);

				var pronunciationSelector = "div.ei-g span.i";
				var $pronunciationList = jQuery("#pronunciations");
				queue(jQuery("#words"), value);

				$(res.responseText).find(pronunciationSelector).each(function () {
					var val = jQuery(this).text().trim();
					if (val.length > 0) {
						queue($pronunciationList, val);
						
					}
				});

				updateForm();
				updateMoreOptions();
			}
			catch (err) {
				return false;
			}
		},
		complete: function () { jQuery("#oxfordAjax").toggle(false); }
	});
}

function searchWordreference(value, callback) {
	url = "http://mini.wordreference.com/mini/index.aspx?dict=encz&w=" + value;

	$.ajax({
		url: url,
		type: 'GET',
		beforeSend: function () {
			jQuery("#wrAjax").toggle(true);
		},
		success: function (res) {
			try {
				var $response = $(res.responseText);
				callback.fire();

				//word
				queue(jQuery("#words"), value);

				var meaningList = jQuery("#meanings");
				var $exampleList = jQuery("#examples");

				//meanings
				var $meanings = $response.find("td.Head:not(#compounds)").closest("tbody");
				if ($meanings.length == 0) {
					$meanings = $response.find("td:not(#compounds)").closest("tbody");
				}

				$meanings.find("td.ToWrd").each(function () {
					queue(meaningList, jQuery(this).justtext());
					//					jQuery('<li>').text(jQuery(this).justtext()).appendTo(meaningList);
				});

				//examples
				$response.find(".FrEx").each(function () {
					var $foreignParent = jQuery(this).closest("tr").next("tr");
					var $native = $foreignParent.find(".ToEx").first();
					if ($native.find("span").length == 0) {
						queuePair($exampleList, [$(this).children("p").first().justtext(), $native.text().trim().replace("or:", "nebo:")]);
					}
				});

				$response.find("#compounds").closest("tbody").find(".FrWrd").each(function () {
					var $native = jQuery(this).closest("tr").find(".ToWrd");
					var foreign = $(this).children("p").first().justtext();
					if (!foreign || foreign.length == 0) {
						foreign = $(this).children("strong").first().justtext();
					}

					if ($native.length > 0) {
						queuePair($exampleList, [foreign, $native.text().trim().replace("or:", "nebo:")]);
					}

				});

				updateForm();
				updateMoreOptions();
			}
			catch (err) {
				return false;
			}

		},
		complete: function () { jQuery("#wrAjax").toggle(false); }
	});
}


function searchPronunciation(value) {
	var $pronunciationList = jQuery("#pronunciations");

	jQuery.ajax({
		url: "/Service/Pronunciation?value=" +value,
		dataType: 'json',
		success: function (response) {
			

			$.each(response, function (index, car) {
				queue($pronunciationList, car);
			});
		}
	});
}

function searchSlovnikOnLine(value) {

	var url = 'http://www.online-slovnik.cz/?translate-type=en-cz&word=' + value;

	$.ajax({
		url: url,
		type: 'GET',
		beforeSend: function () {
			jQuery("#oxfordAjax").toggle(true);
		},
		success: function (res) {
			try {
				var response = $(res.responseText);
				var $meaningList = jQuery("#meanings");

				queue(jQuery("#words"), value);

				$(res.responseText).find("#result ul li").each(function () {
					try {
						var splited = $(this).children("p").first().justtext().substring(2);
						queue($meaningList, splited);
					
					}
					catch (err) {

					}
				});

				updateForm();
				updateMoreOptions();
			}
			catch (err) {
				return false;
			}
		},
		complete: function () { jQuery("#oxfordAjax").toggle(false); }
	});
}

function queue($to, value) {
	value = decodeURIComponent(value.trim());

	$to.queue(function () {
		if (value.length > 0 && ($to.find("a:contains('" + value + "')").length == 0) ) {
			var link = jQuery("<a>").attr("onClick", "toggleMoreOption(this);") .appendTo($to);
			link.text(value);
		}
		$to.dequeue();
	});
}

function queuePair($to, value) {
	value[0] = value[0].replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ');
	value[1] = value[1].replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ');
	$to.queue(function () {
		if (value.length > 0 && ($to.find("a:contains('" + value[0] + "')").length == 0)) {
			var link = jQuery("<a>").attr("onClick", "changeExample(this);").appendTo($to);
			jQuery("<p>").text(value[0]).appendTo(link);
			jQuery("<p>").text(value[1]).appendTo(link);
		}
		$to.dequeue();
	});

}

function fillLi(innerText) {

	return jQuery("<div class='optionItem'>")
					.text(innerText);

	return jQuery("<div class='optionItem'>")
				.append(jQuery("<div class='selector'>")
					.append(jQuery("<a>").text('S')))
				.append(jQuery("<div class='con'>").text(innerText));
}


function createDiv(innerText) {
	return jQuery("<div/>", { text: innerText });
}

function updateForm() {

	if (jQuery("#word").val().length == 0) {
		jQuery("#word").val(jQuery("#words a").first().text());
	}

	if (jQuery("#meaning").val().length == 0) {
		jQuery("#meaning").val(jQuery("#meanings a").first().text());
	}

	if (jQuery("#pronunciation").val().length == 0) {
		jQuery("#pronunciation").val(jQuery("#pronunciations a").first().text());
	}

	if (jQuery("#example").val().length == 0) {
		if (jQuery("#examples").children().length > 0) {
			jQuery("#example").val(jQuery("#examples a p").first().text());
			jQuery("#translation").val(jQuery("#examples p + p").first().text());
		} else {
			jQuery("#example").text(jQuery("#examples").children().first().text());
		}
	}

}


function updateMoreOptions() {

	var $wordEdit = jQuery("#wordMoreOptions");
	var $pronunciationEdit = jQuery("#pronunciationMoreOptions");
	var $exampleEdit = jQuery("#exampleMoreOptions");
	var $meaningEdit = jQuery("#meaningMoreOptions");
	var $translationEdit = jQuery("#translationMoreOptions");

	if (!$wordEdit.hasClass("active")) {
		var x = jQuery("#words a").get();
		if (jQuery.unique(x).length > 1) $wordEdit.addClass("active");
	}

	if (!$pronunciationEdit.hasClass("active")) {
		if (jQuery("#pronunciations").children().length > 1) $pronunciationEdit.addClass("active");
	}

	if (!$exampleEdit.hasClass("active")) {
		if (jQuery.unique(jQuery("#examples").children()).length > 1) {
			$exampleEdit.addClass("active");
		}
	}

	if (!$meaningEdit.hasClass("active")) {
		if (jQuery("#meanings").children().length > 1) $meaningEdit.addClass("active");
	}

	if (!$translationEdit.hasClass("active")) {

		if (jQuery("#examples").children().length > 1) {
			$translationEdit.addClass("active");
		}
	}
}
